import { create } from "zustand";
import { devtools } from "zustand/middleware";

import { SocketMessageType } from "@/layouts/layout1/message-handle/interface";

export interface SocketMessage {
    type: SocketMessageType;
    data: string;
}

interface State {
    latestMessage?: SocketMessage | null;
}

interface Action {
    setLatestMessage: (latestMessage: State["latestMessage"]) => void;
}

export const useMessageStore = create<State & Action>()(
    devtools(
        // 操作方法
        (set) => {
            return {
                latestMessage: null,
                setLatestMessage: (latestMessage: State["latestMessage"]) => {
                    set({
                        latestMessage,
                    });
                },
            };
        },
        // 声明空间
        {
            name: "messageStore",
        },
    ),
);
